# Contributor: Francisco Guerreiro <francisg@fnop.net>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Cameron Banta <cbanta@gmail.com>
# Contributor: Ashley Sommer <ashleysommer@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Cameron Banta <cbanta@gmail.com>
pkgname=rsyslog
pkgver=8.2006.0
pkgrel=1
pkgdesc="Enhanced multi-threaded syslogd with database support and more."
url="https://www.rsyslog.com/"
arch="all !s390x" # limited by czmq
license="Apache-2.0 GPL-3.0-or-later LGPL-3.0-or-later"
options="!check"
makedepends="
	autoconf
	automake
	byacc
	curl-dev
	czmq-dev
	flex
	gnutls-dev
	hiredis-dev
	krb5-dev
	libdbi-dev
	libestr-dev
	libfastjson-dev
	libgcrypt-dev
	liblogging-dev
	liblognorm-dev
	libmaxminddb-dev
	libnet-dev
	librelp-dev
	libtool
	linux-headers
	mariadb-connector-c-dev
	net-snmp-dev
	postgresql-dev
	py3-docutils
	rabbitmq-c-dev
	util-linux-dev
	zlib-dev
	"
install="$pkgname.post-upgrade"
subpackages="$pkgname-doc $pkgname-dbg $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/rsyslog/rsyslog/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.logrotate
	$pkgname.conf
	musl-fix.patch
	queue.patch
	"

# <subpackage>[:<module>...]
_plugins="
	crypto:lmcry_gcry
	elasticsearch:omelasticsearch
	gssapi:lmgssutil:imgssapi:omgssapi
	hiredis:omhiredis
	http:omhttp:fmhttp
	libdbi:omlibdbi
	mmanon
	mmaudit
	mmcount
	mmdblookup
	mmfields
	mmjsonparse
	mmnormalize
	mmpstrucdata
	mmrm1stspace
	mmsequence
	mmsnmptrapd
	mmutf8fix
	mysql:ommysql
	pgsql:ompgsql
	pmaixforwardedfrom
	pmlastmsg
	pmsnare
	rabbitmq:omrabbitmq
	relp:imrelp:omrelp
	snmp:omsnmp
	testing:omtesting
	tls:lmnsd_gtls
	udpspoof:omudpspoof
	uxsock:omuxsock
	zmq:imczmq:omczmq
	imdocker
	clickhouse:omclickhouse
	"
for _i in $_plugins; do
	subpackages="$subpackages $pkgname-${_i%%:*}:_plugin"
done

# secfixes:
#   8.1908.0-r1:
#     - CVE-2019-17040
#     - CVE-2019-17041
#     - CVE-2019-17042

prepare() {
	default_prepare
	autoreconf -fi
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		\
		--disable-rfc3195 \
		--enable-largefile \
		--enable-gssapi-krb5 \
		--enable-mysql \
		--enable-pgsql \
		--enable-libdbi \
		--enable-snmp \
		--enable-elasticsearch \
		--enable-omhttp \
		--enable-clickhouse \
		--enable-gnutls \
		--enable-mail \
		--enable-imdiag \
		--enable-mmnormalize \
		--enable-mmjsonparse \
		--enable-mmaudit \
		--enable-mmanon \
		--enable-mmrm1stspace \
		--enable-mmutf8fix \
		--enable-mmcount \
		--enable-mmsequence \
		--enable-mmdblookup \
		--enable-mmfields \
		--enable-mmpstrucdata \
		--enable-relp \
		--enable-imfile \
		--enable-imptcp \
		--enable-impstats \
		--enable-omprog \
		--enable-omudpspoof \
		--enable-omstdout \
		--enable-pmlastmsg \
		--enable-pmaixforwardedfrom \
		--enable-pmsnare \
		--enable-omuxsock \
		--enable-mmsnmptrapd \
		--enable-omrabbitmq \
		--enable-imczmq \
		--enable-omczmq \
		--enable-omhiredis \
		--enable-imdocker
	make
}

package() {
	make DESTDIR="$pkgdir" install

	# Remove lib that is used only for testing.
	rm -f "$pkgdir"/usr/lib/rsyslog/imdiag.so

	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.conf "$pkgdir"/etc/$pkgname.conf

	install -m700 -d "$pkgdir"/var/lib/rsyslog
}

_plugin() {
	local name="${subpkgname#$pkgname-}"
	pkgdesc="$name support for $pkgname"
	depends="$pkgname=$pkgver-r$pkgrel"

	local libnames=$(printf '%s\n' $_plugins | grep "^$name:" | cut -d: -f2- | tr : ' ')
	local libname

	for libname in ${libnames:-$name}; do
		mkdir -p "$subpkgdir"/usr/lib/rsyslog/
		mv "$pkgdir"/usr/lib/rsyslog/$libname.so "$subpkgdir"/usr/lib/rsyslog/
	done

	# Allow people to find plugins by their full name (including prefix;
	# mm, om, im, ...).
	for libname in $libnames; do
		provides="$provides $pkgname-$libname=$pkgver-r$pkgrel"
	done
}

sha512sums="33d4d9d729d690803e2b02106d7e0f930be78c10fb6c7d05ded739472b5534d5ea66c870b2b7bd348ff107eced68b3d4e34ed53779eadfaf8456cac1d81373b3  rsyslog-8.2006.0.tar.gz
bcd63c8df2ac63b80f3cb51ba7f544988df6cd875f4e81020e762dff30d7537f21b72c95a4b1c08baf15f4ed5f03defbf3f061673aabada5841f45ab9f579374  rsyslog.initd
198ad8f617b9edb93c9231118a9b3bb80b1e00e6517d2a79c393cbfef4417b8f0d08f231fb33843f8e9b09c7f9bc69dd501057ffe9eef583108af34996fee59d  rsyslog.logrotate
517e3c9fced414d397ce946eed50534ea821cd1a7aee2e873d5ffa4c4df3b4433b4f2a2e063825641ce6aaeddd5bd1fd2508ad34099fe30d0d3d615d25121f67  rsyslog.conf
15745c8cdb730ae548d038ca4c04f9f48ef55c6e04949a8e86df356877563c0fcb9660445e47d3f9530925092d6dd80b2b2fc3f64a114ee85103d137327524cb  musl-fix.patch
ef2e000b1c42cb5beffb26393952c2a692791e78972ee4b6f187ca53e338122b2004cc5216381c042195f12cc58f37f186a04e12a65b5bdfdcdf76b73393efb7  queue.patch"
